﻿var TempJsonParameter, TempQuery;

$(function () {
    window.progressDialog = new ProgressDialog();
    window.infoDialog = new InfoDialog();
    window.ajaxClass = new AjaxClass();

    /* 声明各种事件 --------------------------------------------------------------------------------------------*/

    $("#ButtonSave").button({ icons: { primary: "ui-icon-disk"} }).click(function () {
        var TempName, QueryStr, TempTilte;
        TempTilte = $("#MainTable input[name='ViewTitle']").val();
        if (TempTilte != "") {
            var TempJson = {
                "ViewTitle": TempTilte,
                "Operations": {}
            };
            $("#MainTable td[class='Unit Operations']").each(function () {
                TempName = $(this).find("input[name='Key']").val();
                if (TempName != "") {
                    TempJson.Operations[TempName] = {
                        "OperationType": $(this).find("select[name='OperationType']").val(),
                        "Permission": $(this).find("input[name='Permission']").val(),
                        "JsonParameter": $(this).find("input[name='JsonParameter']").val(),
                        "Asynchronous": $(this).find("input[name='Asynchronous']").is(':checked'),
                        "Query": null
                    };
                    QueryStr = $(this).find("input[name='Query']").val();
                    if (QueryStr != "") {
                        TempJson.Operations[TempName].Query = JSON.parse(QueryStr);
                    }
                }
            });

            var JsonSend = { "UserID": JsonOriginal.UserID, "Permissions": JsonOriginal.Permissions, "JsonString": JSON.stringify(TempJson) };
            window.ajaxClass.AjaxSubmit(JsonOriginal.UrlAction, JsonSend, "", "window.location.href =  JsonOriginal.UrlReferrer;", "", false, true);
        } else {
            infoDialog.OpenDialog("视图标题必须填写！", "Alert");
        }
    });
    $("#ButtonBack").button({ icons: { primary: "ui-icon-arrowreturnthick-1-w"} }).click(function () {
        window.location.href = JsonOriginal.UrlReferrer;
    });

    /* 声明和处理对话框 --------------------------------------------------------------------------------------------*/

    $("#JsonParameterDialog").dialog({
        autoOpen: false,
        resizable: false,
        height: 420,
        width: 680,
        modal: true,
        zIndex: 1000,
        buttons: {
            提交: function () {
                $(window.TempJsonParameter).children("input[name='JsonParameter']input[type='hidden']").val($("#JsonParameterTable textarea").val());
                $("#JsonParameterTable textarea").val("");
                $(this).dialog("close");
            },
            取消: function () {
                $(this).dialog("close");
            }
        }
    });
    window.ShowJsonParameterDialog = function (evenObj) {
        window.TempJsonParameter = evenObj;
        $("#JsonParameterTable textarea").val($(evenObj).children("input[name='JsonParameter']input[type='hidden']").val());
        $("#JsonParameterDialog").dialog("open");
    }

    $("#QueryDialog").dialog({
        autoOpen: false,
        resizable: false,
        height: 590,
        width: 800,
        modal: true,
        zIndex: 1000,
        buttons: {
            提交: function () {
                var TempJson = {
                    "QueryString": $("#QueryTable textarea[name='QueryString']").val(),
                    "CountQuery": $("#QueryTable textarea[name='CountQuery']").val(),
                    "Tablename": $("#QueryTable input[name='Tablename']").val(),
                    "CommandType": $("#QueryTable select[name='CommandType']").val(),
                    "Parameters": new Array()
                };
                $("#Parameters div[class='Panel']").each(function () {
                    if ($(this).find("input[name='Name']").val() != "") {
                        TempJson.Parameters.push({ "Name": $(this).find("input[name='Name']").val(), "ParameterType": $(this).find("select[name='ParameterType']").val(), "Value": $(this).find("input[name='Value']").val() });
                    }
                });
                $(window.TempQuery).children("input[name='Query']input[type='hidden']").val(JSON.stringify(TempJson));
                $(this).dialog("close");
            },
            取消: function () {
                $(this).dialog("close");
            }
        }
    });
    window.ShowQueryDialog = function (evenObj) {
        window.TempQuery = evenObj;
        var TempStr = $(window.TempQuery).children("input[name='Query']input[type='hidden']").val();
        if (TempStr != "") {
            var TempJson = JSON.parse(TempStr);
            $("#QueryTable textarea[name='QueryString']").val(TempJson.QueryString);
            $("#QueryTable textarea[name='CountQuery']").val(TempJson.CountQuery);
            $("#QueryTable input[name='Tablename']").val(TempJson.Tablename);
            $("#QueryTable select[name='CommandType']").val(TempJson.CommandType);
            $("#Parameters").html("");
            window.AddParameter();
            for (var i = 0; i < TempJson.Parameters.length; i++) {
                $("#Parameters div[class='Panel']:last").find("input[name='Name']").val(TempJson.Parameters[i].Name);
                $("#Parameters div[class='Panel']:last").find("select[name='ParameterType']").val(TempJson.Parameters[i].ParameterType);
                $("#Parameters div[class='Panel']:last").find("input[name='Value']").val(TempJson.Parameters[i].Value);
                window.AddParameter();
            }
        } else {
            $("#QueryTable textarea[name='QueryString']").val("");
            $("#QueryTable textarea[name='CountQuery']").val("");
            $("#QueryTable input[name='Tablename']").val("DefaultTable");
            $("#QueryTable select[name='CommandType']").val("Text");
            $("#Parameters").html("");
            window.AddParameter();
        }
        $("#QueryDialog").dialog("open");
    }

    /* 添加可活动项目 --------------------------------------------------------------------------------------------*/

    window.AddOperation = function () {
        if ($("#MainTable input[name='Key']input[value='']").size() == 0) {
            $("#MainTable tbody:first").children("tr:last-child").before("<tr><td class=\"Unit Operations\" onmouseover=\"$(this).css('background-color','#F7F7F7')\" onmouseout=\"$(this).css('background-color','transparent')\">" + document.getElementById("OperationsHTML").innerHTML + "</td></tr>");
        }
    }

    window.AddParameter = function () {
        if ($("#QueryTable input[name='Name']input[value='']").size() == 0) {
            $("#Parameters").append(document.getElementById("ParametersHTML").innerHTML);
        }
    }

    window.DeleteOperation = function (evenObj) {
        $(evenObj.parentNode.parentNode.parentNode).remove();
        window.AddOperation();
    }

    window.DeleteParameter = function (evenObj) {
        $(evenObj.parentNode.parentNode.parentNode.parentNode.parentNode).remove();
        window.AddParameter();
    }

    /* 页面初始化填充 --------------------------------------------------------------------------------------------*/

    $("#MainTable input[name='ViewTitle']").val(JsonOriginal.ViewTitle);
    if (JsonOriginal.Operations != null) {
        window.AddOperation();
        for (var n in JsonOriginal.Operations) {
            $("#MainTable td[class='Unit Operations']:last").find("input[name='Key']").val(n);
            $("#MainTable td[class='Unit Operations']:last").find("select[name='OperationType']").val(JsonOriginal.Operations[n].OperationType);
            $("#MainTable td[class='Unit Operations']:last").find("input[name='Permission']").val(JsonOriginal.Operations[n].Permission);
            $("#MainTable td[class='Unit Operations']:last").find("input[name='JsonParameter']").val(JsonOriginal.Operations[n].JsonParameter);
            if (JsonOriginal.Operations[n].Asynchronous) {
                $("#MainTable td[class='Unit Operations']:last").find("input[name='Asynchronous']").attr("checked", 'true');
            }
            if (JsonOriginal.Operations[n].Query != null) {
                $("#MainTable td[class='Unit Operations']:last").find("input[name='Query']").val(JSON.stringify(JsonOriginal.Operations[n].Query));
            }
            window.AddOperation();
        }
    } else {
        window.AddOperation();
    }

    $("#MainTable tbody").sortable({ handle: ".ItemHandle", cancel: ".Disabled", items: "tr:not(.Disabled)" });
});